#!/usr/bin/env python
# -*— coding: utf-8 -*-
from multiprocessing import Pool, Manager
import os

def copyFileTask(name, oldFolderName, newFolderName, queue):
  # 完成一个拷贝文件的功能
  fr = open(oldFolderName+'/'+name)
  fw = open(newFolderName+'/'+name, 'w+')
  content = fr.read()
  fw.write(content)

  fr.close()
  fw.close()

  queue.put(name)


def main():
  # 获取要copy文件夹名字
  oldFolderName = input('请输入文件夹名字：')
  oldFolderName = '/Users/rainhowchan/Desktop/' + oldFolderName
  # 创建一个文件夹
  newFolderName = oldFolderName + '-复件'
  # print(newFolderName)
  os.mkdir(newFolderName)

  # 获取old文件夹里所有文件的名字
  fileNames = os.listdir(oldFolderName)
  print(fileNames)
  # 使用多进程的方式copy 源文件到新文件夹
  pool = Pool(3)
  queue = Manager().Queue()

  for name in fileNames:
    pool.apply_async(copyFileTask, args=(name,oldFolderName,newFolderName, queue))

  num = 0
  allNum = len(fileNames)

  while num < allNum:
    fileName = queue.get()
    num += 1
    copyRate = num/allNum
    print('\rcopy的进度是%.2f%%'%(copyRate*100), end='')
    #print('---%d---%s'%(num,fileName))

  print('\n已完成拷贝')
#  pool.close() # 不再接收新的任务
 # pool.join() # 主进程等待子进程执行完

if __name__ == '__main__':
  main()
